<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2018/2/5
 * Time: 14:29
 */

namespace Common\Model;


class MemberModel extends CommonModel
{

    const DISABLE       = 'disable';//禁用
    const ABLE          = 'able';//启用

    //自动验证
    protected $_validate = [
        //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
        ['username', 'require', '用户名必填！', 1, 'regex', CommonModel:: MODEL_INSERT],
        ['password', '6,20', '密码长度为6-20位', 0, 'length', CommonModel:: MODEL_UPDATE],
    ];


    /**
     * 注册
     *
     * @param $username
     * @param $password
     * @param $parent_mid
     * @return bool
     */
    public function register($username, $password)
    {
        if (empty($username) && empty($password))
            return false;

        $data = [
            'username'    => $username,
            'password'    => sp_password($password),
            'create_time' => date('Y-m-d H:i:s', time()),
            'update_time' => date('Y-m-d H:i:s', time()),
        ];
        $this->startTrans();
        $rst_1 = $this->add($data);
//        dump($rst_1.'/'.$rst_2.'/'.$rst_3);exit;
        if ($rst_1 && $rst_2) {
            $this->commit();
            return true;
        } else {
            $this->rollback();
            return false;
        }
    }



    /**
     *
     * @param $username
     *
     * @return mixed
     */
    public function checkUserName($phone)
    {
        return $this->where(['phone' => $phone])->count();
    }

    /**
     * @param $mid
     * @return bool
     * 退出登录
     */
    public function userLoginOut($mid)
    {
        $result = $this
            ->where(['id' => $mid])
            ->save([
                'token'          => '',
                'token_time_out' => '',
            ]);
        if ($result === false) return false;
        return true;
    }

    /**
     * @param $level
     * @return string
     * 用户等级
     */
    public function levelToString($level) {
        switch ($level) {
            case self::LEVEL_1:
                return '注册用户';
            case self::LEVEL_2:
                return '消费商';
            case self::LEVEL_3:
                return '总监';
            default:
                return '未知';
        }
    }

    /**
     * @param $mid
     * @return bool
     * 检查是否是初次购买,初次购买参与返利
     */
    public function checkFirstBuy($mid) {
        if($this->where(['id' => $mid])->getField('if_buy') == 'no') {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param $mid
     * @return bool
     * 修改为已购买过商品（即非第一次）
     */
    public function changeBuyStatus($mid) {
        return $this->where(['id' => $mid])->setField('if_buy', 'yes');
    }

    /**
     * 获取支付密码
     * @param $mid
     * @return mixed
     */
    public function getPayPassword($mid) {
        return $this->where(['id' => $mid])->getField('pay_password');
    }

    /**
     * 检查密码
     *
     * @param $id
     * @param $password
     *
     * @return bool
     */
    public function check_user_password($id, $password)
    {
        $pay_world = $this->where(['id' => $id])->getField('pay_password');
        return sp_compare_password($password, $pay_world);
    }

    /**
     * @param $mid
     * @return bool
     * 检查禁用情况
     */
    public function checkStatus($mid) {
        if($this->where(['id' => $mid])->getField('status') == self::DISABLE) {
            return false;
        } else {
            return true;
        }
    }
}